<?php


class FileTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('File');
    }

    /*
     * $documentId must be not empty
     * Returns only that files which has not been deleted(is_deleted=false). 
     */
    static public function findAllExistingFilesOfDocument($documentId)
    {
        if (!empty($documentId))
        {
          return FileTable::getInstance()
            ->createQuery('f')
            ->select('f.*')
            ->andWhere("f.document_id=?", $documentId)
            ->andWhere("f.is_deleted=?", false)
            ->execute();
        }
        return null;
    }

    static public function setLastLoaderQuery($fileId,$userId)
    {
        if (!empty($fileId))
        {
           $q = Doctrine_Query::create()
            ->update('File')
            ->set('lastDownloadId','?',$userId)
            ->where('id =?',$fileId)
            ->execute();
        }
    }

    /*
     * $documentId must be not empty
     * Returns all files from input document. 
     */
    static public function findAllFilesOfDocument($documentId)
    {
        if (!empty($documentId))
        {
          return FileTable::getInstance()
            ->createQuery('f')
            ->select('f.*')
            ->where("f.document_id=?", $documentId)
            ->execute();
        }
        return null;
    }
}